<template>
  <div class="campus-enterprise-nvibar">
    <div>
      <!-- 企业信息描述 -->
      <el-descriptions
        class="margin-top"
        :title="enterpriseInfoForm.ename"
        :column="2"
        :size="size"
      >
        <template slot="extra">
          <el-button type="primary" size="small" @click="enterpriseUploadMethod"
            >公司入驻</el-button
          >
          <el-button
            type="success"
            size="small"
            @click="dialogUpdateFormVisible = true"
            >更新</el-button
          >
        </template>
        <el-descriptions-item
          class="enterprise-descriptions"
          label="公司名称"
          >{{ enterpriseInfoForm.ename }}</el-descriptions-item
        >
        <el-descriptions-item class="enterprise-descriptions" label="创建时间">
          <el-date-picker
            :readonly="true"
            v-model="enterpriseInfoForm.buildtime"
            style="width: 140px"
            type="date"
            placeholder="无"
          >
          </el-date-picker>
        </el-descriptions-item>
        <el-descriptions-item class="enterprise-descriptions" label="创始人">{{
          enterpriseInfoForm.founder
        }}</el-descriptions-item>
        <el-descriptions-item class="enterprise-descriptions" label="年龄">{{
          enterpriseInfoForm.fonderage
        }}</el-descriptions-item>
        <el-descriptions-item
          class="enterprise-descriptions"
          label="公司地址"
          >{{ enterpriseInfoForm.eaddress }}</el-descriptions-item
        >
        <el-descriptions-item class="enterprise-descriptions" label="人数">{{
          enterpriseInfoForm.enumbers
        }}</el-descriptions-item>
        <el-descriptions-item
          class="enterprise-descriptions"
          label="公司性质"
          >{{ enterpriseInfoForm.eclass }}</el-descriptions-item
        >
        <el-descriptions-item class="enterprise-descriptions" label="简介">{{
          enterpriseInfoForm.edesc
        }}</el-descriptions-item>
        <el-descriptions-item
          class="enterprise-descriptions"
          label="企业文化"
          >{{ enterpriseInfoForm.corporateculture }}</el-descriptions-item
        >
      </el-descriptions>
      <!-- 企业入驻对话框 -->
      <el-dialog
        title="企业入驻"
        :visible.sync="dialogFormVisible"
        :modal="false"
        append-to-body
      >
        <el-form
          :model="enterpriseUploadForm"
          :inline="true"
          ref="enterpriseUploadFormRef"
          label-width="formLabelWidth"
        >
          <el-form-item label="公司名称" :label-width="formLabelWidth">
            <el-input
              v-model="enterpriseUploadForm.ename"
              autocomplete="off"
              style="width: 150px"
            ></el-input>
          </el-form-item>
          <el-form-item label="创建时间" :label-width="formLabelWidth">
            <el-date-picker
              v-model="enterpriseUploadForm.buildtime"
              style="width: 150px"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="创始人" :label-width="formLabelWidth">
            <el-input
              v-model="enterpriseUploadForm.founder"
              style="width: 150px"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="年龄" :label-width="formLabelWidth">
            <el-input
              v-model.number="enterpriseUploadForm.fonderage"
              style="width: 150px"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="公司地址" :label-width="formLabelWidth">
            <el-input
              v-model="enterpriseUploadForm.eaddress"
              style="width: 150px"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="人数" :label-width="formLabelWidth">
            <el-input
              v-model="enterpriseUploadForm.enumbers"
              style="width: 150px"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="公司性质" :label-width="formLabelWidth">
            <el-input
              v-model="enterpriseUploadForm.eclass"
              style="width: 150px"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="简介" :label-width="formLabelWidth">
            <el-input
              v-model="enterpriseUploadForm.edesc"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="企业文化" :label-width="formLabelWidth">
            <el-input
              v-model="enterpriseUploadForm.corporateculture"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="enterpriseUpload">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 企业更新对话框 -->
      <el-dialog
        title="更新"
        :visible.sync="dialogUpdateFormVisible"
        :modal="false"
        append-to-body
      >
        <el-form
          :model="enterpriseInfoForm"
          :inline="true"
          ref="enterpriseInfoFormRef"
          label-width="formLabelWidth"
        >
          <el-form-item label="公司名称" :label-width="formLabelWidth">
            <el-input
              v-model="enterpriseInfoForm.ename"
              autocomplete="off"
              style="width: 150px"
            ></el-input>
          </el-form-item>
          <el-form-item label="创建时间" :label-width="formLabelWidth">
            <el-date-picker
              v-model="enterpriseInfoForm.buildtime"
              style="width: 150px"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="创始人" :label-width="formLabelWidth">
            <el-input
              v-model="enterpriseInfoForm.founder"
              style="width: 150px"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="年龄" :label-width="formLabelWidth">
            <el-input
              v-model.number="enterpriseInfoForm.fonderage"
              style="width: 150px"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="公司地址" :label-width="formLabelWidth">
            <el-input
              v-model="enterpriseInfoForm.eaddress"
              style="width: 150px"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="人数" :label-width="formLabelWidth">
            <el-input
              v-model="enterpriseInfoForm.enumbers"
              style="width: 150px"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="公司性质" :label-width="formLabelWidth">
            <el-input
              v-model="enterpriseInfoForm.eclass"
              style="width: 150px"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="简介" :label-width="formLabelWidth">
            <el-input
            type="textarea"
              v-model="enterpriseInfoForm.edesc"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="企业文化" :label-width="formLabelWidth">
            <el-input
            type="textarea"
              v-model="enterpriseInfoForm.corporateculture"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogUpdateFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="enterpriseUpdate">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  name: "RecommendEnterpriseinfo",

  data() {
    return {
      userId: "",
      size: "medium",
      // 企业信息表单
      enterpriseInfoForm: {
        eid: 0,
        ename: "",
        buildtime: new Date(),
        founder: "",
        fonderage: "",
        eaddress: "",
        enumbers: "",
        eclass: "",
        edesc: "",
        corporateculture: "",
      },
      // 企业入驻表单
      enterpriseUploadForm: {
        ename: "",
        buildtime: new Date(),
        founder: "",
        fonderage: "",
        eaddress: "",
        enumbers: "",
        eclass: "",
        edesc: "",
        corporateculture: "",
      },
      // 企业查询对象
      queryEnterpriseParams: {
        hname: "",
      },
      dialogFormVisible: false,
      dialogUpdateFormVisible: false,
      formLabelWidth: "120px",
      isSettle: "",
    };
  },

  mounted() {
    //查询企业信息
    this.queryEnterpriseInfo();
  },

  methods: {
    // 获取用户登录信息
    getUserInfo() {
      this.$axios
        .post("/jwt/getUserInfo")
        .then((res) => {
          var response = res.data.desc;
          if (response) {
            this.queryEnterpriseParams.hname = response;
          } else {
            this.$message({
              message: "获取失败",
              type: "error",
            });
          }
        })
        .catch((error) => {
          console.log(error);
          this.$message({
            message: "系统错误!!!",
            type: "error",
          });
        });
    },
    // 判断公司是否已经存在
    enterpriseUploadMethod() {
      if (this.enterpriseInfoForm.eid > 0) {
        //如果已经存在
        this.$message({
          message: "只能入驻一个企业",
          type: "error",
        });
        return;
      } else {
        //   打开对话框
        this.dialogFormVisible = true;
      }
    },
    // 公司入驻方法
    enterpriseUpload() {
      // 发送请求入驻企业
      this.$axios
        .post("/enterpriseInfo/enterpriseInfoSettle", this.enterpriseUploadForm)
        .then((res) => {
          var response = res.data.desc;
          if (response === "企业入驻失败") {
            this.$message({
              message: "企业入驻失败,亲重新试一下",
              type: "error",
            });
          } else if (response === "企业入驻成功") {
            this.$message({
              message: "企业入驻成功",
              type: "success",
            });
            // 加载企业信息
            this.queryEnterpriseInfo();
            //   关闭对话框
            this.dialogFormVisible = false;
          }
        })
        .catch((error) => {
          console.log(error);
          this.$message({
            message: "系统错误!!!",
            type: "error",
          });
        });
    },
    // 查询企业信息
    queryEnterpriseInfo() {
      this.queryEnterpriseParams.hname = this.userId;
      this.$axios
        .post("/enterpriseInfo/queryEnterpriseInfo", this.queryEnterpriseParams)
        .then((res) => {
          if (res.data.result != null) {
            this.enterpriseInfoForm = res.data.result;
          } else {
            return;
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    // 企业更新
    enterpriseUpdate() {
      this.$axios
        .post("/enterpriseInfo/updateEnterprise", this.enterpriseInfoForm)
        .then((res) => {
          var response = res.data.desc;
          if (response === "更新失败") {
            this.$message({
              message: "亲,更新失败,请重试",
              type: "error",
            });
          } else if (response === "更新成功") {
            this.$message({
              message: "更新成功",
              type: "success",
            });
            // 关闭对话框
            this.dialogUpdateFormVisible = false;
            // 重新加载数据
            this.queryEnterpriseInfo();
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
  },
};
</script>

<style scoped>
.campus-enterprise-nvibar {
  width: 1150px;
  height: 565px;
}

h1 {
  margin: 0px;
  padding: 0px;
}

.margin-top {
  margin-left: 15px;
}
</style>
